package com.tyqx.hashchainutil.common.mongodb;

import com.tyqx.hashchainbean.vo.ImageVO;
import com.tyqx.hashchainutil.common.EmptyUtils1;
import org.hyperledger.fabric.sdk.security.CryptoPrimitives;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.Base64;
import java.util.List;

public class ImagePathHashValueUtil {

    public static String getHashValue(String hashValueStr, CryptoPrimitives suite) throws Exception {
//        CryptoPrimitives suite = new CryptoPrimitives();
//        suite.init();
        //Base64 Encoded
        byte[] hashBytes=suite.hash(hashValueStr.getBytes());
        String hashValue = Base64.getEncoder().encodeToString(hashBytes);
        return hashValue;
    }
    
    public static ImageVO insertAttachment(String attachmentStr, MongoTemplate mongoTemplate, String collectionName, String imageHashValue) throws Exception{

        ImageVO imageVO = new ImageVO();
        imageVO.setHashValue(imageHashValue);
        imageVO.setAttachment(attachmentStr);
        try {
            Criteria criteria = Criteria.where("hashValue").is(imageHashValue);
            Query query = Query.query(criteria);
            List<ImageVO> imageVOList = mongoTemplate.find(query, ImageVO.class, collectionName);
            if(EmptyUtils1.isEmpty(imageVOList)){
                mongoTemplate.insert(imageVO, collectionName);
            }
        } catch (Exception e) {
            System.out.println("error message: " + e.getMessage());
            if (!e.getMessage().startsWith("E11000 duplicate key error collection")){
                System.out.println("error insert mongo");
                e.printStackTrace();
                throw e;
            }
        }
        return imageVO;
    }


    public static String insertAttachment(String attachmentStr, MongoTemplate mongoTemplate, String collectionName) throws Exception{
        CryptoPrimitives suite = new CryptoPrimitives();
        suite.init();
        String imageHashValue=getHashValue(attachmentStr,suite);
        ImageVO imageVO = new ImageVO();
        imageVO.setHashValue(imageHashValue);
        imageVO.setAttachment(attachmentStr);
        try {
            Criteria criteria = Criteria.where("hashValue").is(imageHashValue);
            Query query = Query.query(criteria);
            List<ImageVO> imageVOList = mongoTemplate.find(query, ImageVO.class, collectionName);
            if(EmptyUtils1.isEmpty(imageVOList)){
                mongoTemplate.insert(imageVO, collectionName);
            }
        } catch (Exception e) {
            System.out.println("error message: " + e.getMessage());
            if (!e.getMessage().startsWith("E11000 duplicate key error collection")){
                System.out.println("error insert mongo");
                e.printStackTrace();
                throw e;
            }
        }
        return imageHashValue;
    }
}
